package com.example.imitation_webchat_demo.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Pattern;

import com.example.imitation_webchat_demo.model.Resource;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class AppUtils {
	// 获取版本名称
	public static String getVersionName(Context context) {
		return getPackageInfo(context).versionName;
	}

	// 获取版本号
	public static int getVersionCode(Context context) {
		return getPackageInfo(context).versionCode;
	}
	
	/*
	 * 获取包相关信息
	 */
	public static PackageInfo getPackageInfo(Context context) {
		PackageInfo pi = null;
		try {
			PackageManager pm = context.getPackageManager();
			pi = pm.getPackageInfo(context.getPackageName(),
					PackageManager.GET_CONFIGURATIONS);
			return pi;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return pi;
	}
	
	public static void showToast(Context context, String str){
		Toast toast = null;
		toast = Toast.makeText(context, str, Toast.LENGTH_LONG);
		toast.show();
	}
	
	// 使view控件获取焦点
	public static void setFocus(View view) {
		view.setFocusable(true);
		view.setFocusableInTouchMode(true);
		view.requestFocus();
		view.requestFocusFromTouch();
	}
	
	/*
	 * 隐藏软键盘
	 */
	public static void hideSoftInputWindow(Context context, EditText editText) {
		InputMethodManager imm = (InputMethodManager) context
				.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
	}
	
	// 判断字符是否为字母或者数字格式
	public static boolean isCharacterOrDigital(String str) {
		Pattern pattern = Pattern.compile("[0-9a-zA-Z]*");
		return pattern.matcher(str).matches();
	}
	
	// 验证是否为邮箱格式
	public static boolean isEmail(String str) {
		Pattern pattern = Pattern
				.compile("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");
		return pattern.matcher(str).matches();
	}
	
	// 判断是否有中文
	public static boolean containChineseChar(String str) {
		Pattern pattern = Pattern.compile(".*[\\u4e00-\\u9faf].*");
		return pattern.matcher(str).matches();
	}
	
	// iv控件中设置图片源，源为BPUtil中createBitmap方法得到的bitmap对象
	public static void setImageResource(ImageView imageView){
		BPUtils.getInstance().clearBitmap();
		imageView.setImageBitmap(BPUtils.getInstance().createBitmap());
	}
	
	// 得到pattern格式的时间字符串
	public static String getFormatDateMsg(String pattern, Date date){
		DateFormat format =SimpleDateFormat.getInstance();
		return format.format(date);
	}
	
	//从sharepreference中得到username
	public static String getUsernameFromShare(Context context){
		String username = null;
		username = context.getSharedPreferences(Resource.appName, Context.MODE_PRIVATE).getString("username", null);
		return username;
	}
}